using JGSY.CMS.LowCode.Platform.Application.DTOs;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces
{
    /// <summary>
    /// 部门应用服务接口
    /// 提供部门管理相关的业务逻辑操作
    /// </summary>
    public interface IDepartmentAppService
    {
        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <returns>部门列表</returns>
        Task<IEnumerable<DepartmentDto>> GetAllAsync();

        /// <summary>
        /// 根据ID获取部门
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <returns>部门信息</returns>
        Task<DepartmentDto?> GetByIdAsync(long id);

        /// <summary>
        /// 根据编码获取部门
        /// </summary>
        /// <param name="code">部门编码</param>
        /// <returns>部门信息</returns>
        Task<DepartmentDto?> GetByCodeAsync(string code);

        /// <summary>
        /// 获取子部门列表
        /// </summary>
        /// <param name="parentId">父部门ID</param>
        /// <returns>子部门列表</returns>
        Task<IEnumerable<DepartmentDto>> GetChildrenAsync(long parentId);

        /// <summary>
        /// 获取部门树形结构
        /// </summary>
        /// <returns>部门树形结构</returns>
        Task<IEnumerable<DepartmentDto>> GetHierarchyAsync();

        /// <summary>
        /// 获取用户所属部门路径
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>部门路径</returns>
        Task<IEnumerable<DepartmentDto>> GetUserDepartmentPathAsync(long userId);

        /// <summary>
        /// 分页获取部门列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="parentId">父部门ID</param>
        /// <param name="status">状态过滤</param>
        /// <returns>分页部门列表</returns>
        Task<(IEnumerable<DepartmentDto> Items, int TotalCount)> GetPagedAsync(
            int pageIndex, 
            int pageSize, 
            string? keyword = null, 
            long? parentId = null,
            byte? status = null);

        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="departmentDto">部门信息</param>
        /// <returns>创建的部门</returns>
        Task<DepartmentDto> CreateAsync(DepartmentDto departmentDto);

        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="departmentDto">部门信息</param>
        /// <returns>更新的部门</returns>
        Task<DepartmentDto> UpdateAsync(DepartmentDto departmentDto);

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <returns>是否删除成功</returns>
        Task<bool> DeleteAsync(long id);

        /// <summary>
        /// 批量删除部门
        /// </summary>
        /// <param name="ids">部门ID列表</param>
        /// <returns>删除的数量</returns>
        Task<int> BatchDeleteAsync(IEnumerable<long> ids);

        /// <summary>
        /// 更新部门状态
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <param name="status">状态</param>
        /// <returns>是否更新成功</returns>
        Task<bool> UpdateStatusAsync(long id, byte status);

        /// <summary>
        /// 移动部门
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <param name="newParentId">新父部门ID</param>
        /// <returns>是否移动成功</returns>
        Task<bool> MoveDepartmentAsync(long id, long? newParentId);

        /// <summary>
        /// 设置部门负责人
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <param name="managerId">负责人ID</param>
        /// <returns>是否设置成功</returns>
        Task<bool> SetManagerAsync(long id, long? managerId);

        /// <summary>
        /// 检查部门编码是否存在
        /// </summary>
        /// <param name="code">部门编码</param>
        /// <param name="excludeId">排除的部门ID</param>
        /// <returns>是否存在</returns>
        Task<bool> CodeExistsAsync(string code, long? excludeId = null);

        /// <summary>
        /// 检查是否可以删除部门
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <returns>是否可以删除</returns>
        Task<bool> CanDeleteAsync(long id);

        /// <summary>
        /// 获取部门用户列表
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <param name="includeChildren">是否包含子部门</param>
        /// <returns>用户列表</returns>
        Task<IEnumerable<UserDto>> GetDepartmentUsersAsync(long id, bool includeChildren = false);

        /// <summary>
        /// 获取部门统计信息
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <returns>统计信息</returns>
        Task<object> GetDepartmentStatisticsAsync(long id);

        /// <summary>
        /// 搜索部门
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>搜索结果</returns>
        Task<(IEnumerable<DepartmentDto> Items, int TotalCount)> SearchAsync(
            string keyword, 
            int pageIndex = 1, 
            int pageSize = 20);

        /// <summary>
        /// 同步部门层级和路径
        /// </summary>
        /// <returns>是否同步成功</returns>
        Task<bool> SyncHierarchyAsync();
    }
}
